package com.wzcl.app.dao.repository;

import com.wzcl.app.dao.model.Cart;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface CartRepository extends JpaRepository<Cart,Integer>, JpaSpecificationExecutor<Cart> {

    //校验购物车是否已存在商品
    @Query(value = "SELECT m FROM Cart m WHERE m.userId = ?1 AND m.commodity.id = ?2  AND m.userPhone = ?3 AND m.spec IS NULL AND m.state = 0")
    Cart findByUserIdAndCommodityIdAndNullSpec(Integer user, Integer commodityId, String userPhone);

    @Query(value = "SELECT m FROM Cart m WHERE m.userId = ?1 AND m.commodity.id = ?2 AND m.spec = ?3 AND m.userPhone = ?4 AND m.state = 0")
    Cart findByUserIdAndCommodityIdAndSpec(Integer user, Integer commodityId, String spec, String userPhone);

    List<Cart> findAllByOrderId(Integer id);

    Cart findByIdAndUserPhone(Integer id, String userPhone);


}
